﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ZYO
{
    public class 六爻Rules
    {
        private List<Persistence.Obj.六爻规则> metRules;
        public List<Persistence.Obj.六爻规则> 满足的规则
        {
            get { return metRules; }
        }

        Persistence.Obj.六爻起卦 gua;

        public 六爻Rules()
        {
            metRules = new List<Persistence.Obj.六爻规则>();
        }

        public 六爻Rules(Persistence.Obj.六爻起卦 卦)
            : this()
        {
            this.gua = 卦;
            CheckRules();
        }

        private void CheckRules()
        {
            //绝处逢生
            if (this.gua.用神.爻之月建状态.CompareTo(Persistence.Obj.e月建旺相.休囚) <= 0
                && this.gua.用神.日建之生克 == Persistence.Obj.e五行生克关系.被生)
            {
                this.metRules.Add(Persistence.Obj.六爻规则.绝处逢生);
            }

            //用神持世
            if (this.gua.用神 == this.gua.六爻s[this.gua.世爻])
            {
                this.metRules.Add(Persistence.Obj.六爻规则.用神持世);
            }

            foreach (byte yaoid in this.gua.元神s)
            {
                if (this.gua.六爻s[yaoid].Persistence爻.动否 && this.gua.六爻s[yaoid].可用否 == Persistence.Obj.e有用无用.有用)
                {
                    //元神动而被合住, //	爻的化出爻	应在冲化出爻的日子
                    if (this.gua.六爻s[yaoid].动爻变化之冲合 == Persistence.Obj.e地支冲合关系.合)
                    {
                        this.metRules.Add(Persistence.Obj.六爻规则.元神动而被合住);
                    }
                    //元神动空化空,   //	皆应冲空实空之日而有用也
                    if (this.gua.六爻s[yaoid].旬空 && this.gua.六爻s[yaoid].变爻.旬空)
                    {
                        this.metRules.Add(Persistence.Obj.六爻规则.元神动空化空);
                    }
                }
            }

            foreach (byte yaoid in this.gua.忌神s)
            {
                if (this.gua.六爻s[yaoid].Persistence爻.动否 && this.gua.六爻s[yaoid].可用否 == Persistence.Obj.e有用无用.有用)
                {
                    //忌神动而被合住, //	应在冲化出爻的日子
                    if (this.gua.六爻s[yaoid].动爻变化之冲合 == Persistence.Obj.e地支冲合关系.合)
                    {
                        this.metRules.Add(Persistence.Obj.六爻规则.忌神动而被合住);
                    }

                    //忌神动空化空,   //	皆应冲空实空之日而有用也
                    if (this.gua.六爻s[yaoid].旬空 && this.gua.六爻s[yaoid].变爻.旬空)
                    {
                        this.metRules.Add(Persistence.Obj.六爻规则.忌神动空化空);
                    }
                }
            }

            //六合,相合法有六：
            //日月合爻者一也。
            if (this.gua.用神.月建之冲合 == Persistence.Obj.e地支冲合关系.合)
            {

            }
            if (this.gua.用神.日建之冲合 == Persistence.Obj.e地支冲合关系.合)
            {

            }
            //爻与爻合者二也。
            if (null != this.gua.用神.动爻之合我 && this.gua.用神.动爻之合我.Length > 0)
            {

            }
            //爻动化合者三也，
            if (this.gua.用神.Persistence爻.动否 && this.gua.用神.动爻变化之冲合 == Persistence.Obj.e地支冲合关系.合)
            {

            }

            //卦逢六合四也。
            switch (ZYOShared.Check地支关系(this.gua.六爻s[this.gua.世爻].Persistence爻.地支de枚举值, this.gua.六爻s[this.gua.应爻].Persistence爻.地支de枚举值))
            {
                //六合
                case Persistence.Obj.e地支冲合关系.合:
                    this.metRules.Add(Persistence.Obj.六爻规则.六合); //六合
                    if (0 != this.gua.本卦.变卦记录ID) //有动爻
                    {
                        Runtime六十四卦 the变卦 = new Runtime六十四卦(this.gua.本卦.变卦de枚举值, 0);
                        switch (ZYOShared.Check地支关系(the变卦.Xml.世爻.地支de枚举值, the变卦.Xml.应爻.地支de枚举值))
                        {
                            case Persistence.Obj.e地支冲合关系.合:

                                this.metRules.Add(Persistence.Obj.六爻规则.六合化六合);//六合卦变六合卦六也。
                                break;
                            case Persistence.Obj.e地支冲合关系.冲:
                                this.metRules.Add(Persistence.Obj.六爻规则.六合化六冲);
                                break;

                        }
                    }
                    break;
                //六冲
                case Persistence.Obj.e地支冲合关系.冲:
                    this.metRules.Add(Persistence.Obj.六爻规则.六冲);//六冲
                    if (0 != this.gua.本卦.变卦记录ID) //有动爻
                    {
                        Runtime六十四卦 the变卦 = new Runtime六十四卦(this.gua.本卦.变卦de枚举值, 0);
                        switch (ZYOShared.Check地支关系(the变卦.Xml.世爻.地支de枚举值, the变卦.Xml.应爻.地支de枚举值))
                        {
                            case Persistence.Obj.e地支冲合关系.合:

                                this.metRules.Add(Persistence.Obj.六爻规则.六冲化六合);//六冲卦变六合卦五也。
                                break;
                            case Persistence.Obj.e地支冲合关系.冲:
                                this.metRules.Add(Persistence.Obj.六爻规则.六冲化六冲);
                                break;

                        }
                    }

                    break;
                default:
                    if (0 != this.gua.本卦.变卦记录ID) //有动爻
                    {
                        Runtime六十四卦 the变卦 = new Runtime六十四卦(this.gua.本卦.变卦de枚举值, 0);
                        switch (ZYOShared.Check地支关系(the变卦.Xml.世爻.地支de枚举值, the变卦.Xml.应爻.地支de枚举值))
                        {
                            case Persistence.Obj.e地支冲合关系.合:
                                this.metRules.Add(Persistence.Obj.六爻规则.化六合);
                                break;
                            case Persistence.Obj.e地支冲合关系.冲:
                                this.metRules.Add(Persistence.Obj.六爻规则.化六冲);
                                break;

                        }
                    }

                    break;
            }

        }
    }

}
